package com.waypolice.manager.service.impl;

import com.waypolice.common.constant.BorrowConstant;
import com.waypolice.manager.service.BorMyService;
import com.waypolice.mapper.CmsBorRegisterMapper;
import com.waypolice.mapper.handwriting.CmsBorRegisterExtMapper;
import com.waypolice.pojo.CmsBorRegister;
import com.waypolice.pojo.CmsBorRegisterExample;
import com.waypolice.pojo.vo.CmsBorRegisterVo;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 描述:
 *
 * @author zyp
 * @description 我的借阅service实现类
 * @create 2019-03-12 16:17
 */
@Service
public class BorMyServiceImpl implements BorMyService {

    private static final Logger logger = LoggerFactory.getLogger(BorRegisterServiceImpl.class);

    @Autowired
    private CmsBorRegisterMapper cmsBorRegisterMapper;

    @Autowired
    private CmsBorRegisterExtMapper cmsBorRegisterExtMapper;

    /**
     * @author:zyp
     * @description 获取我的借阅信息
     * @date: 2019/3/13 9:20
     * @return 
     */
    @Override
    public Map<String,Object> getBorrowMy(int offset, int limit, String search, Integer userId) {
        Map<String,Object> parameterMap = new HashedMap();
//        updateBorStatusUP(userId);
        parameterMap.put("userId",(long)userId);
        List<Map<String,String>>  pageList = cmsBorRegisterExtMapper.getBorrowMy(parameterMap);
        BorRegisterServiceImpl borRegisterService = new BorRegisterServiceImpl();
        pageList = borRegisterService.resetRegistList(pageList);
        int count = cmsBorRegisterExtMapper.coutBorrowMy(parameterMap);
        Map<String,Object> result = new HashedMap(2);
        result.put("rows",pageList);
        result.put("total",count);
        return result;
    }

    /**
     * @author:zyp
     * @description 处理分页问题
     * @date: 2019/3/13 9:20
     * @return 
     */
    private List<CmsBorRegisterVo> pageByList(int offset, int limit ,String search ,List<CmsBorRegisterVo> lists){
        //临时的list集合
        List<CmsBorRegisterVo>  provisionalList =  new ArrayList<CmsBorRegisterVo>();
        //最终的list集合
        List<CmsBorRegisterVo> finalList = new ArrayList<CmsBorRegisterVo>();
        if(StringUtils.isNotBlank(search)){
            for (CmsBorRegisterVo cmsBorRegisterVo : lists){
                if (cmsBorRegisterVo.getBorFileName()!=null&&cmsBorRegisterVo.getBorFileName().contains(search)){
                    provisionalList.add(cmsBorRegisterVo);
                }
            }
        }else{
            provisionalList = lists;
        }
        if((limit+offset)<=provisionalList.size()){
            while(offset<limit+offset){
                finalList.add(provisionalList.get(offset));
                offset++;
            }
        }else{
            while(offset<provisionalList.size()){
                finalList.add(provisionalList.get(offset));
                offset++;
            }
        }
        return finalList;
    }

    /**
     * @author zyp
     * @description 将线上的，已过期的档案进行修改状态，已归还
     * @date 2018/5/22 16:09
     * @param
     * @return
     */
    @Override
    public void updateBorStatusUP(Integer userId){
        //查找之前，先将该用户的线上借阅的档案筛选，将已过期的档案进行修改状态，已归还
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        //修改档案登记的内容
        CmsBorRegister cmsBorRegister = new CmsBorRegister();
        cmsBorRegister.setBorStatus(3);
        CmsBorRegisterExample cmsBorRegisterExample = new CmsBorRegisterExample();
        CmsBorRegisterExample.Criteria criteria =  cmsBorRegisterExample.createCriteria();
        if(userId!=null){
            criteria.andBorUserIdEqualTo((long)userId);
        }
        criteria.andBorTypeEqualTo(0L);
        criteria.andPlanReturnTimeLessThan(cal.getTime());
        criteria.andRegisterNumIsNotNull();
        criteria.andBorStatusEqualTo(BorrowConstant.BORROW_STATUS_BOR);
        cmsBorRegisterMapper.updateByExampleSelective(cmsBorRegister,cmsBorRegisterExample);
    }

    /**
     * @author:zyp
     * @description 归还授权的线上借阅文件（修改状态为已归还）
     * @date: 2019/3/13 9:23
     * @return 
     */
    @Override
    public int returnAccreditArch(String ids) {
        int count = 0;
        if (StringUtils.isBlank(ids)) {
            return count;
        }else{
            String[] idArray = ids.split("-");
            for (String idStr : idArray) {
                if (StringUtils.isBlank(idStr)) {
                    continue;
                }
                Long id = Long.parseLong(idStr);
                CmsBorRegister cmsBorRegister = new CmsBorRegister();
                cmsBorRegister.setId(id);
                cmsBorRegister.setBorStatus(3);
                int result = cmsBorRegisterMapper.updateByPrimaryKeySelective(cmsBorRegister);
                count += result;
            }
            return count;
        }
    }
}